package com.example.administrator.netchess;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MainActivity extends Activity {
    private ListAdapter listAdapter;
    private ListView listView;
    private ArrayList<ListItem> arrayList;
    private Button button_build,button_askfor;
    private MyService.OptBinder optBinder = null;
    private JSONArray ja ;
    private ListItem listItem;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case Common.FinalString.MESSAGE_MAIN:

                    break;

                case Common.FinalString.MESSAGE_MAIN_LIST:
                    ja = (JSONArray) msg.obj;
                    arrayList.clear();
                    JSONObject jsonObject = new JSONObject();
                    try {
                        if (null==ja){
                            return;
                        }
                        else {
                            for (int i = 0;i<ja.length();i++){
                                jsonObject = ja.getJSONObject(i);
                                int room_id= jsonObject.getInt(Common.FinalString.KEY_ROOM_ID);
                                int player_number = jsonObject.getInt(Common.FinalString.PLAYER_NUMBER);
                                String room_name = jsonObject.getString(Common.FinalString.KEY_ROOM_NAME);

                                listItem = new ListItem(room_name,room_id,player_number);
                                arrayList.add(listItem);
                            }
                            listAdapter.notifyDataSetChanged();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

                case Common.FinalString.MESSAGE_EMPTY:
                    Toast.makeText(MainActivity.this, "加入房间失败,请重新寻找房间", Toast.LENGTH_SHORT).show();
                    break;

                case Common.FinalString.MESSAGE_INTENT_ROOM:
                    Intent intent = new Intent(MainActivity.this,RoomActivity.class);
                    startActivity(intent);
                       break;


                default:
                    break;
            }

        }
    };

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
         optBinder = (MyService.OptBinder) service;
            optBinder.setHandle(handler);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
         optBinder = null;
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.room_list);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (null!=optBinder){
                    JSONObject jo = new JSONObject();
                try {
                    jo.put(Common.FinalString.CMD,Common.JsonCmdReq.APPLY_FOR_JOIN_ROOM);
                    jo.put(Common.FinalString.KEY_NAME,Common.FinalString.MY_NAME);
                    jo.put(Common.FinalString.KEY_ROOM_ID, arrayList.get(position).getRoom_id());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                    System.out.println("--"+jo.toString());
                    optBinder.sendData(jo.toString());

                }

            }
        });


        Intent intent = new Intent(MainActivity.this,MyService.class);
        bindService(intent,conn,MyService.BIND_AUTO_CREATE);

        button_build = (Button)findViewById(R.id.button_build);
        button_askfor = (Button)findViewById(R.id.button_askfor);

        button_build.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (null!=optBinder){

                    JSONObject jo = new JSONObject();
                    try {
                        jo.put(Common.FinalString.CMD,Common.JsonCmdReq.CREATE_ROOM);
                        jo.put(Common.FinalString.KEY_NAME,Common.FinalString.MY_NAME);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    optBinder.sendData(jo.toString());

                }
            }
        });

        button_askfor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (null!=optBinder){

                    JSONObject jo = new JSONObject();
                    try {
                        jo.put(Common.FinalString.CMD,Common.JsonCmdReq.APPLY_FOR_ROOM_LIST);
                        jo.put(Common.FinalString.KEY_NAME,Common.FinalString.MY_NAME);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    optBinder.sendData(jo.toString());

                }

            }
        });

        arrayList = new ArrayList<>();
        listAdapter = new ListAdapter(MainActivity.this,arrayList);
        listView.setAdapter(listAdapter);

    }


    @Override
    protected void onResume() {

        Intent intent = new Intent(MainActivity.this,MyService.class);
        bindService(intent,conn,MyService.BIND_AUTO_CREATE);
        if (null!=optBinder){
            optBinder.setHandle(handler);
        }
        super.onResume();
    }


    @Override
    protected void onStop() {
        if (null!=conn){
            unbindService(conn);
        }
        super.onStop();
    }
}
